import { getToken,setToken,removeToken} from '@/utils/auth'
import { set } from 'nprogress'
import { login as loginApi } from '@/api/common'
import { getUserInfo as getUserInfoApi } from '@/api/user'
const state = {
  token: getToken(),
  userinfo: {},
  userId:localStorage.getItem('id')
}
const mutations = {
  updateToken(state, token) { 
    state.token = token
    setToken(token)
  },
  updateuserinfo(state, payload) { 
    state.userinfo = payload
  },
  delToken(state) { 
    state.token = null
    removeToken()
  }
}
const actions = {
 async login(context, data) { 
    // console.log(commit,data);
    const res = await loginApi(data)
    console.log(res);
    localStorage.setItem('id',res.userId) 
    context.commit('updateToken',res.token)
  
  },
  async getUserInfo({ commit, state }) { 
    // console.log(state.userid);
    
    const res1 = await getUserInfoApi(state.userId)
    console.log(res1);
    commit('updateuserinfo',res1)
  },
  async logout({ commit}) { 
    commit('delToken')
  }
}
export default {
   namespaced: true,
   state,
   mutations,
   actions
}

